//
//  AppointOrderDetailViewController.m
//  customer
//
//  Created by Holothurian on 2018/11/5.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "AppointOrderDetailViewController.h"
#import "SelfInfoLabelTableViewCell.h"
#import "AppointOrderDetailTableViewCell.h"
#import "DecorateAppointMultiTableViewCell.h"

static NSString *DefaultCellID = @"DefaultCellID";
static NSString *PositionCellID = @"PositionCellID";
static NSString *OthersCellID = @"OthersCellID";
@interface AppointOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)NSMutableArray *keyArray;

@property (nonatomic,strong)NSMutableArray *valueArray;

@end

@implementation AppointOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createMainView];
    [self loadData];
}

- (void)loadData{
    AutoWeakSelf
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:0];
    [dict setObject:[NSString emptyStr:K_UserDefault_Read(CustomerUserID)] forKey:@"member_id"];
    [dict setObject:[NSString emptyStr:K_UserDefault_Read(CustomerUserToken)] forKey:@"member_token"];
    [dict setObject:self.order_id?:@"" forKey:@"order_id"];
    /*
     0 装修预约详情
     1 设计预约详情
     2 加配预约详情
     3 装修订单详情
     4 加配订单详情
     */
    NSString *requestURL = nil;
    if (self.selectIndex == 0 || self.selectIndex == 1) {
        requestURL = kGetRenovationOrderDetail;
    }else if (self.selectIndex == 2){//加配预约订单
        requestURL = kGetCustomizedOrderDetail;
    }else if (self.selectIndex == 3){//装修订单、根据order_type判断 含有设计订单
        //order_type;//1:装修预约 2:设计预约
        requestURL = kGetMaintailOrderDetail;
    }else if (self.selectIndex == 4){//加配订单详情
        requestURL = kGetCustomizedOrderDetail;
    }
    [self showLoading];
    [[QYNetManager sharedManager]POST:requestURL parameters:dict andApiTyp:CustomerApiTypGetOrders SuccessBlock:^(id responseObject) {
        [weakSelf hideLoading];
        [weakSelf endReresh];
        id resultData = responseObject[@"data"];
        if ([responseObject[@"status"] isEqualToString:@"error"]) {
            [weakSelf showErrorTipWithTitle:responseObject[@"error"]];
        }else if (![resultData isKindOfClass:[NSNull class]] && [resultData isKindOfClass:[NSDictionary class]] && [resultData count]){
            if ([responseObject[@"total"] integerValue]<10 || ([responseObject[@"total"] integerValue] == self.dataList.count)) {
                [weakSelf.tableView.mj_footer removeFromSuperview];
            }
            self.orderModel = [DecorateInsertOrderModel yy_modelWithDictionary:resultData];
            if (self.selectIndex == 0 || self.selectIndex == 1 || self.selectIndex == 3) {
                if (self.selectIndex == 0) {
                    self.title = @"装修预约详情";
                }else if (self.selectIndex == 1) {
                    self.title = @"设计预约详情";
                }else{
                    if ([self.orderModel.order_type isEqualToString:@"1"]) {
                        self.title = @"装修订单详情";
                    }else{
                        self.title = @"设计订单详情";
                    }
                }
                self.keyArray = [NSMutableArray arrayWithArray:@[@"订单编号",@"下单时间",@"选择类型",@"房屋面积",@"联系人",@"联系电话",@"所在地区",@"详细地址",@"工程造价"]];
                self.valueArray = [NSMutableArray arrayWithArray:
                                   @[self.orderModel.order_no?:@"",self.orderModel.create_time?:@"",
                                     self.orderModel.style_name?:@"",self.orderModel.order_area?:@"",
                                     self.orderModel.contact_name?:@"",self.orderModel.contact_mobile?:@"",
                                     [NSString stringWithFormat:@"%@%@%@",self.orderModel.address_provider,self.orderModel.address_city,self.orderModel.address_country]?:@"",
                                     self.orderModel.address_detail?:@"",self.orderModel.order_price?:@""]];
            }
            else
            {
                self.addModel = [CustomAddModel yy_modelWithDictionary:resultData];
                self.keyArray = [NSMutableArray arrayWithArray:@[@"项目名称",@"户型选择",@"方案选择",@"方案报价",@"联系人",@"联系电话",@"所在地区",@"详细地址"]];
                
                if (self.selectIndex == 4) {
                    self.title = @"加配订单详情";
                    self.valueArray = [NSMutableArray arrayWithArray:
                                       @[self.addModel.customized_name?:@"",self.addModel.huxing_name?:@"",
                                         self.addModel.programme_name?:@"",self.addModel.order_price?:@"",
                                         self.addModel.contact_name?:@"",self.addModel.contact_mobile?:@"",
                                         [NSString stringWithFormat:@"%@%@%@",self.addModel.address_provider,self.addModel.address_city,self.addModel.address_country]?:@"",
                                         self.addModel.address_detail?:@""]];
                    [self.keyArray addObject:@"工程造价"];
                    [self.valueArray addObject:self.addModel.order_price?:@"" ];
                }else{
                    self.valueArray = [NSMutableArray arrayWithArray:
                                       @[self.addModel.customized_name?:@"",self.addModel.huxing_name?:@"",
                                         self.addModel.programme_name?:@"",self.addModel.order_price?:@"",
                                         self.addModel.contact_name?:@"",self.addModel.contact_mobile?:@"",
                                         [NSString stringWithFormat:@"%@%@%@",self.addModel.address_provider,self.addModel.address_city,self.addModel.address_country]?:@"",
                                         self.addModel.address_detail?:@""]];
                    self.title = @"加配预约详情";
                }
            }
            [weakSelf.tableView reloadData];
            weakSelf.tableView.showEmpty = NO;
        }else if (![resultData isKindOfClass:[NSNull class]] && [resultData isKindOfClass:[NSDictionary class]] && [resultData count] == 0){
            [weakSelf.dataList removeAllObjects];
            weakSelf.tableView.showEmpty = YES;
            [weakSelf.tableView reloadData];
        }
    } Failure:^(NSError *error) {
        [weakSelf endReresh];
        [weakSelf hideLoading];
        weakSelf.tableView.showEmpty = YES;
        [weakSelf showErrorTipWithTitle:CustomerPoolNetStr];
    }];
}

- (void)createMainView{
   
    AutoWeakSelf
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.view).offset(NAVIGATION_BAR_HEIGHT);
        make.height.mas_offset(F_DEVICE_H - NAVIGATION_BAR_HEIGHT- bottomPadding(0));
    }];
    [self.tableView registerClass:[SelfInfoLabelTableViewCell class] forCellReuseIdentifier:DefaultCellID];
    [self.tableView registerClass:[DecorateAppointPositionSelectTableCell class] forCellReuseIdentifier:PositionCellID];
    [self.tableView registerClass:[AppointOrderDetailTableViewCell class] forCellReuseIdentifier:OthersCellID];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.keyArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //元、m²
    if (indexPath.row == 3 || indexPath.row == 8) {
        AppointOrderDetailTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:OthersCellID];
        cell.nameLabel.text = self.keyArray[indexPath.row];
        cell.nameLabel.textColor = self.selectIndex == 2?[HSPublic colorWithHexString:@"B2B2B2"]:[HSPublic colorWithHexString:@"333333"];
        cell.detailNameLabel.text = self.valueArray[indexPath.row]?:@"";
        cell.detailNameLabel.textColor = (self.selectIndex == 2 || indexPath.row == 8)?[HSPublic colorWithHexString:@"#D51A28"]:[HSPublic colorWithHexString:@"333333"];
        cell.unitLabel.text = (self.selectIndex == 2 || indexPath.row == 8)?@"元":@"m²";
        return cell;
    }
    //地址
    else if(indexPath.row == 6){
        DecorateAppointPositionSelectTableCell *cell = [self.tableView dequeueReusableCellWithIdentifier:PositionCellID];
        cell.nameLabel.text = self.keyArray[indexPath.row];
        cell.nameLabel.textColor = self.selectIndex == 2?[HSPublic colorWithHexString:@"B2B2B2"]:[HSPublic colorWithHexString:@"333333"];
        cell.selectPlaceLabel.text = [NSString emptyStr:self.valueArray[indexPath.row]];
        cell.selectPlaceLabel.textColor = [HSPublic colorWithHexString:@"333333"];
        return cell;
    }else{
        SelfInfoLabelTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:DefaultCellID];
        cell.nameLabel.text = self.keyArray[indexPath.row];
        cell.nameLabel.textColor = self.selectIndex == 2?[HSPublic colorWithHexString:@"#B2B2B2"]:[HSPublic colorWithHexString:@"333333"];
        cell.detailNameLabel.text = self.valueArray[indexPath.row]?:@"";
        cell.detailNameLabel.textColor = [HSPublic colorWithHexString:@"333333"];
        cell.detailNameLabel.textAlignment = NSTextAlignmentLeft;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50.f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.selectIndex == 3 || self.selectIndex == 4) {
        return 50.f;
    }
    return 20.f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, F_DEVICE_W, 20)];
    backView.backgroundColor = [UIColor appColor];
    if (self.selectIndex == 3||self.selectIndex == 4) {
        backView.frame = CGRectMake(0, 0, F_DEVICE_W, 50);
        backView.backgroundColor = [UIColor whiteColor];
        UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(15, 5, F_DEVICE_W - 30, 40)];
        topView.backgroundColor = [UIColor whiteColor];
        [backView addSubview:topView];
        NSMutableArray *stateArray = [NSMutableArray arrayWithArray:@[@"预约成功",@"线下确认",@"定金",@"进度款",@"尾款",@"完成"]];
        NSInteger orderDetailIndex = 0;
        CGFloat equalWidth = (F_DEVICE_W - 30)/6.0;
        CGFloat equalHeight = 40;
        if (self.selectIndex == 4) {
            [stateArray removeObjectAtIndex:3];
            orderDetailIndex = [self.addModel.order_state integerValue];
            equalWidth = (F_DEVICE_W - 30)/5.0;
        }else{
            orderDetailIndex = [self.orderModel.order_state integerValue];
        }
        
        for (NSInteger i = 0; i < [stateArray count]; i++) {
            UIButton *stateButton = [UIButton buttonWithType:UIButtonTypeCustom];
            stateButton.titleLabel.font = Font(9);
            [stateButton setTitleColor:[HSPublic colorWithHexString:@"333333"] forState:UIControlStateNormal];
            [stateButton setTitleColor:[HSPublic colorWithHexString:@"999999"] forState:UIControlStateSelected];
            [stateButton setImageVertical:[UIImage imageNamed:@"orderDetailDone"] withTitle:stateArray[i] forState:UIControlStateNormal];
            [stateButton setImageVertical:[UIImage imageNamed:@"orderDetailUnfinished"] withTitle:stateArray[i] forState:UIControlStateSelected];
            [topView addSubview:stateButton];
            [stateButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(topView);
                make.left.equalTo(topView).offset(equalWidth * i-1);
                make.size.mas_offset(CGSizeMake(equalWidth, equalHeight));
            }];
            if (i > orderDetailIndex) {
                stateButton.selected = YES;
                [stateButton setImageVertical:[UIImage imageNamed:@"orderDetailUnfinished"] withTitle:stateArray[i] forState:UIControlStateNormal |UIControlStateSelected];
            }else{
                stateButton.selected = NO;
                [stateButton setImageVertical:[UIImage imageNamed:@"orderDetailDone"] withTitle:stateArray[i] forState:UIControlStateNormal |UIControlStateSelected];
            }
        }
    }
    return backView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
